
package com.eyedog.uilibrary.tabs;

import android.content.res.ColorStateList;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.eyedog.uilibrary.R;
import com.eyedog.uilibrary.base.BaseActivity;

public class TabsActivity extends BaseActivity {

    protected ViewStub mStubBottom;

    protected LinearLayout mTabLayout;

    protected FrameLayout mTab1, mTab2, mTab3;

    protected View mIndicate1, mIndicate2, mIndicate3;

    protected TextView mTv1, mTv2, mTv3;

    @Override
    protected void onContentCreated(ViewStub contentLayout) {
        mStubBottom = contentLayout;
        mStubBottom.setLayoutResource(R.layout.layout_tabs);
        mTabLayout = (LinearLayout) mStubBottom.inflate();
        mTab1 = (FrameLayout) findViewById(R.id.fl_tab1);
        mTab2 = (FrameLayout) findViewById(R.id.fl_tab2);
        mTab3 = (FrameLayout) findViewById(R.id.fl_tab3);
        mIndicate1 = findViewById(R.id.indicate_tab1);
        mIndicate2 = findViewById(R.id.indicate_tab2);
        mIndicate3 = findViewById(R.id.indicate_tab3);
        int[] pressedStates = new int[] { android.R.attr.state_selected };
        int[] normalStates = new int[] {};
        int[][] states = new int[][] { pressedStates, normalStates };
        int[] colors = new int[] {
            getResources().getColor(R.color.tab_text_color),
            getResources().getColor(R.color.half_text)
        };
        mIndicate1.setBackgroundTintList(new ColorStateList(states, colors));
        mIndicate2.setBackgroundTintList(new ColorStateList(states, colors));
        mIndicate3.setBackgroundTintList(new ColorStateList(states, colors));
        mTv1 = (TextView) findViewById(R.id.tv_tab1);
        mTv2 = (TextView) findViewById(R.id.tv_tab2);
        mTv3 = (TextView) findViewById(R.id.tv_tab3);
        mTab1.setOnClickListener(mTabListener);
        mTab2.setOnClickListener(mTabListener);
        mTab3.setOnClickListener(mTabListener);
        setupTabsView();
    }

    protected void setupTabsView() {
        mTv1.setText("视频");
        mTv2.setText("游戏");
        mTv3.setText("会员");
        if (getDefaultTab() == 0) {
            mTab1.performClick();
        } else if (getDefaultTab() == 1) {
            mTab2.performClick();
        } else if (getDefaultTab() == 2) {
            mTab3.performClick();
        }
    }

    protected int getDefaultTab() {
        return 0;
    }

    View.OnClickListener mTabListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.fl_tab1) {
                refreshTabSelected(v.getId());
                onSelecteTab1();
            } else if (v.getId() == R.id.fl_tab2) {
                refreshTabSelected(v.getId());
                onSelecteTab2();
            } else if (v.getId() == R.id.fl_tab3) {
                refreshTabSelected(v.getId());
                onSelecteTab3();
            }
        }
    };

    protected void refreshTabSelected(int id) {
        mIndicate1.setSelected(id == R.id.fl_tab1);
        mIndicate2.setSelected(id == R.id.fl_tab2);
        mIndicate3.setSelected(id == R.id.fl_tab3);
        mTv1.setTextColor(
            id == R.id.fl_tab1 ? getResources().getColor(R.color.tab_text_color)
                : getResources().getColor(R.color.half_text));
        mTv2.setTextColor(
            id == R.id.fl_tab2 ? getResources().getColor(R.color.tab_text_color)
                : getResources().getColor(R.color.half_text));
        mTv3.setTextColor(
            id == R.id.fl_tab3 ? getResources().getColor(R.color.tab_text_color)
                : getResources().getColor(R.color.half_text));
    }

    protected void onSelecteTab1() {

    }

    protected void onSelecteTab2() {

    }

    protected void onSelecteTab3() {

    }
}
